package com.infra.micro.id.controller;

import com.infra.common.resp.PageEx;
import com.infra.common.resp.Response;
import com.infra.micro.id.domain.entity.IdSegmentPO;
import com.infra.micro.id.domain.service.IdSegmentService;
import com.infra.micro.id.model.req.QueryIdSegmentByCondReq;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Set;

@RestController
@RequestMapping("/api/id/segment")
@Api(tags = {"号段算法api服务"})
public class IdSegmentController {
    @Resource
    private IdSegmentService idSegmentService;

    @ApiOperation(value = "指定appId下生成一个ID", notes = "指定appId下生成一个ID", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appId", value = "应用ID", dataType = "Integer", dataTypeClass = Integer.class, paramType = "path", required = true),
            @ApiImplicitParam(name = "secretKey", value = "密钥KEY(如验证配置已打开则为必传参)", dataType = "String", dataTypeClass = String.class, paramType = "query")
    })
    @GetMapping("/{appId}")
    public Response<Long> getId(@PathVariable("appId") Integer appId, String secretKey) {
        return Response.success(idSegmentService.getId(appId, secretKey));
    }

    @ApiOperation(value = "指定appId下生成一批ID", notes = "指定appId下生成一批ID", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appId", value = "应用ID", dataType = "Integer", dataTypeClass = Long.class, paramType = "path", required = true),
            @ApiImplicitParam(name = "step", value = "步长", dataType = "Integer", dataTypeClass = Integer.class, paramType = "path", required = true),
            @ApiImplicitParam(name = "secretKey", value = "密钥KEY(如验证配置已打开则为必传参)", dataType = "String", dataTypeClass = String.class, paramType = "query")
    })
    @GetMapping("/{appId}/{step}")
    public Response<Set<Long>> getId(@PathVariable("appId") Integer appId, @PathVariable("step") int step, String secretKey) {
        return Response.success(idSegmentService.getIds(appId, step, secretKey));
    }

    @ApiOperation(value = "查询应用号段配置列表(分页)", notes = "查询应用号段配置列表(分页)", produces = "application/json")
    @GetMapping("/list")
    public Response<PageEx<IdSegmentPO, QueryIdSegmentByCondReq>> list(QueryIdSegmentByCondReq req) {
        return idSegmentService.list(req);
    }
}
